package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exeception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author SunSun
 * @date 2021/10/28
 */
@Service
@Slf4j
public class CommentService {

    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public PageResult findComments(Integer page, Integer pagesize, String movementId) {
        List<Comment> list=commentApi.findComments(movementId,CommentType.COMMENT,page,pagesize);

        if(CollUtil.isEmpty(list)){
            return new PageResult();
        }

        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        Map<Long, UserInfo> byIds = userInfoApi.findByIds(userIds, null);

        List<CommentVo> lists=new ArrayList<>();

        for (Comment comment : list) {
            UserInfo userInfo = byIds.get(comment.getUserId());
            if(userInfo!=null){
                CommentVo commentVo=CommentVo.init(userInfo,comment);
                //修复点赞bug
                String key= Constants.MOVEMENTS_INTERACT_KEY+comment.getId();
                String hashkey=Constants.MOVEMENT_LIKE_HASHKEY+UserHolder.getUserId();

                if(redisTemplate.opsForHash().hasKey(key,hashkey)){
                    commentVo.setHasLiked(1);
                }
                lists.add(commentVo);
            }
        }

        return new PageResult(page,pagesize,0l,lists);
    }

    public void saveComment(String comment, String movementId) {
        Long userId = UserHolder.getUserId();

        Comment comment1=new Comment();
        comment1.setPublishId(new ObjectId(movementId));
        comment1.setUserId(userId);
        comment1.setCommentType(CommentType.COMMENT.getType());
        comment1.setContent(comment);
        comment1.setCreated(System.currentTimeMillis());

        Integer commentCount=commentApi.save(comment1);
        log.info("commentCount="+commentCount);
    }

    public Integer likecomment(String ID) {
        Long userId = UserHolder.getUserId();

        boolean hasComment=commentApi.likecomment(userId,ID,CommentType.LIKE);

        if(hasComment){
            throw new BusinessException(ErrorResult.likeError());
        }

        Comment comment=new Comment();
        comment.setPublishId(new ObjectId(ID));
        comment.setUserId(userId);
        comment.setCommentType(CommentType.LIKE.getType());
        comment.setCreated(System.currentTimeMillis());

        Integer count = commentApi.save(comment);

        String key= Constants.MOVEMENTS_INTERACT_KEY+ID;
        String hashkey=Constants.MOVEMENT_LIKE_HASHKEY+userId;
        redisTemplate.opsForHash().put(key,hashkey,"1");
        return count;
    }

    public Integer dislikecomment(String movementID) {
        Long userId = UserHolder.getUserId();
        boolean hasComment=commentApi.likecomment(userId,movementID,CommentType.LIKE);
        if(!hasComment){
            throw new BusinessException(ErrorResult.disLikeError());
        }

        Comment comment=new Comment();
        comment.setPublishId(new ObjectId(movementID));
        comment.setUserId(userId);
        comment.setCommentType(CommentType.LIKE.getType());

        Integer count = commentApi.delete(comment);
        String key= Constants.MOVEMENTS_INTERACT_KEY+movementID;
        String hashkey=Constants.MOVEMENT_LIKE_HASHKEY+userId;
        redisTemplate.opsForHash().delete(key,hashkey);
        return count;
    }

    public Integer lovecomment(String movementID) {
        Long userId = UserHolder.getUserId();
        boolean hasComment=commentApi.likecomment(userId,movementID,CommentType.LOVE);
        if(hasComment){
            throw new BusinessException(ErrorResult.disloveError());
        }

        Comment comment=new Comment();
        comment.setPublishId(new ObjectId(movementID));
        comment.setUserId(userId);
        comment.setCommentType(CommentType.LOVE.getType());
        comment.setCreated(System.currentTimeMillis());
        Integer count = commentApi.save(comment);

        String key= Constants.MOVEMENTS_INTERACT_KEY+movementID;
        String hashkey=Constants.MOVEMENT_LOVE_HASHKEY+userId;
        redisTemplate.opsForHash().put(key,hashkey,"1");
        return count;
    }

    public Integer dislovecomment(String movementID) {

        Long userId = UserHolder.getUserId();
        boolean hasComment=commentApi.likecomment(userId,movementID,CommentType.LOVE);
        if(!hasComment){
            throw new BusinessException(ErrorResult.disloveError());
        }

        Comment comment=new Comment();
        comment.setPublishId(new ObjectId(movementID));
        comment.setUserId(userId);
        comment.setCommentType(CommentType.LOVE.getType());

        Integer count = commentApi.delete(comment);
        String key= Constants.MOVEMENTS_INTERACT_KEY+movementID;
        String hashkey=Constants.MOVEMENT_LOVE_HASHKEY+userId;
        redisTemplate.opsForHash().delete(key,hashkey);
        return count;
    }

    public Integer commentlike(String commentId) {
        Long userId = UserHolder.getUserId();

        boolean hasComment=commentApi.likecomment(userId,commentId,CommentType.LIKE);

        if(hasComment){
            throw new BusinessException(ErrorResult.likeError());
        }

        Comment comment=new Comment();
        comment.setPublishId(new ObjectId(commentId));
        comment.setUserId(userId);
        comment.setCommentType(CommentType.LIKE.getType());
        comment.setCreated(System.currentTimeMillis());

        Integer count = commentApi.save1(comment);

        String key= Constants.MOVEMENTS_INTERACT_KEY+commentId;
        String hashkey=Constants.MOVEMENT_LIKE_HASHKEY+userId;
        redisTemplate.opsForHash().put(key,hashkey,"1");
        return count;
    }

    public Integer commentdislike(String commentId) {
        Long userId = UserHolder.getUserId();
        boolean hasComment=commentApi.likecomment(userId,commentId,CommentType.LIKE);
        if(!hasComment){
            throw new BusinessException(ErrorResult.disLikeError());
        }

        Comment comment=new Comment();
        comment.setPublishId(new ObjectId(commentId));
        comment.setUserId(userId);
        comment.setCommentType(CommentType.LIKE.getType());

        Integer count = commentApi.delete1(comment);
        String key= Constants.MOVEMENTS_INTERACT_KEY+commentId;
        String hashkey=Constants.MOVEMENT_LIKE_HASHKEY+userId;
        redisTemplate.opsForHash().delete(key,hashkey);
        return count;
    }
}
